<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="pemployeeing-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyWord" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="Serch">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="username" label="用户名" width="120" sortable>
			</el-table-column>
			<el-table-column prop="email" label="邮箱" width="120" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="100" sortable>
				<template scope="scope">
					<span style="color: green" v-if="scope.row.state === 1">启用</span>
					<span style="color: red" v-else-if="scope.row.state === 0">禁用</span>
					<span v-else>未知</span>
				</template>
			</el-table-column>
			<el-table-column prop="phone" label="电话" width="180" sortable>
			</el-table-column>
			<el-table-column prop="age" label="年龄" width="80" sortable>
			</el-table-column>
			<el-table-column prop="password" label="密码" width="180" sortable>
			</el-table-column>
			<el-table-column prop="salt" label="盐值" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :current-page="currentPage"
						   :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增/修改界面-->
		<el-dialog :title="title" :visible.sync="employeeFormVisible" :close-on-click-modal="false">
			<el-form :model="employeeForm" label-width="80px" :rules="employeeFormRules" ref="employeeForm">
				<el-form-item label="用户名" prop="username">
					<el-input v-model="employeeForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="employeeForm.password" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="email">
					<el-input v-model="employeeForm.email" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="年龄" prop="age">
					<el-input v-model="employeeForm.age" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话" prop="phone">
					<el-input v-model="employeeForm.phone" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="盐值" prop="salt">
					<el-input v-model="employeeForm.salt" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="employeeForm.state">
						<el-radio class="radio" :label="1">启用</el-radio>
						<el-radio class="radio" :label="0">禁用</el-radio>
					</el-radio-group>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="employeeFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="employeeSubmit" :loading="employeeLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getEmployeeListPage, removeEmployee, batchRemoveEmployee, editEmployee, employeeEmployee } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					keyWord: ''
				},
				employees: [],
				total: 0,
                pageSize: 5,
                currentPage: 1,
                listLoading: false,
				sels: [],//列表选中列

				employeeFormVisible: false,//新增、修改界面是否显示
				employeeLoading: false,//等待框
				employeeFormRules: {//数据校验
					username: [
						{ required: true, message: '请输入用户名', trigger: 'blur' }
					],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ]
				},
				//新增修改界面数据
				employeeForm: {
				    id:null,
					username: '',
                    password:  '',
					email:null,
                    phone:null,
                    age:null,
                    salt:null,
                    state: 0,
				}

			}
		},
		methods: {
            Serch(){
                this.currentPage = 1;
                this.getEmployees();

            },
			//获取最新当前页数，刷新页面
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getEmployees();
			},
			//获取员工列表
			getEmployees() {
				let para = {
				    //将我获得的当前页数  以及显示条数获取并重新赋值
					//高级查询的名字 queery内记得添加字段 跟着分页的数据一起传递
					//上面将高级查询的name 改为了keyword  下面这里重新复制也要改
                    currentPage: this.currentPage,
					pageSize: this.pageSize,
                    keyWord: this.filters.keyWord

				};
				this.listLoading = false;
				//NProgress.start();
				// getEmployeeListPage(para).then((res) => {
				// 	this.total = res.data.total;
				// 	this.employees = res.data.employees;
				// 	this.listLoading = false;
				// 	//NProgress.done();
				// });
				this.$http.post("/employee",para)
					.then(result=>{
						//取出对象中对应的数据
                        this.total = result.data.total;
                        this.employees = result.data.rows;
                        //每次成功加载关闭忙等
                        this.listLoading = false;
					})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/employee/"+row.id)
						.then(result=>{
						    //关闭忙等
                            this.listLoading = false;
                            //后端返回ajax对象
                            result = result.data;
                            if(result.success){
                                this.$message({
									message: result.msg,
									type: 'success'
								})
							}else {
                                this.$message({
                                    message: result.msg,
                                    type: 'error'
                                });
                            };
							//定位到第一页
							this.currentPage = 1;
							//刷新页面
							this.getEmployees();
						})
						.catch(result=>{
						    //如果删除失败  打印一下错误原因
							console.log(result);
							this.$message({
								message: "删除失败",
								type: 'error'
							})
						})

				}).catch(() => {//点击取消就不管了

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
			    this.title = "修改";
				this.employeeFormVisible = true;//弹出模态框   上面为false 重新赋值就打开
				this.employeeForm = Object.assign({}, row);//克隆表格数据 回显
				this.getdeptTree();
			},
			//显示新增界面
			handleAdd: function () {
                this.title = "新增";
				this.employeeFormVisible = true;//弹出模态框
				this.employeeForm = {//清空表格数据
                    id:null,
                    username: '',
                    password: '',
                    email:null,
                    phone:null,
                    age:null,
                    salt:null,
                    state: 0,
				};
			},
			//新增修改方法
			employeeSubmit: function () {
			    //validate失去焦点校验表格数据  无异常才能提交
				this.$refs.employeeForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.employeeLoading = true;//按钮忙等开启
							//将表格的数据赋值给employee
							let employee = Object.assign({}, this.employeeForm);
							//发送请求
							this.$http.put("/employee",employee).
							then(result=>{
                                this.employeeLoading = false;//按钮忙等关闭
                                result = result.data;
                                if(result.success){
                                    this.$message({
                                        message: result.msg,
                                        type: 'success'
                                    })
								}else {
                                    this.$message({
                                        message: result.msg,
                                        type: 'error'
                                    });
								}
                                this.employeeFormVisible = false;//关闭模态框
                                this.getEmployees();//重新查询
							})
							.catch(result=>{
                                this.$message({
                                    message: "操作失败",
                                    type: 'error'
                                });
                            })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
			    //将当前点击获得的id装入数组
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
                    this.listLoading = true;
                    this.$http.patch("/employee",ids)
                        .then(result=>{
                            //关闭忙等框
                            this.listLoading = false;
                            //后端返回ajax对象
                            result = result.data;
                            if(result.success){
                                this.$message({
                                    message: result.msg,
                                    type: 'success'
                                })
                            }else {
                                this.$message({
                                    message: result.msg,
                                    type: 'error'
                                });
							};
                            //定位到第一页
                            this.currentPage = 1;
                            //刷新页面
                            this.getEmployees();
                        })
                        .catch(result=>{
                            //如果删除失败  打印一下错误原因
                            this.$message({
                                message: "删除失败",
                                type: 'error'
                            })
                        })
				})
					.catch(() => {//这是点击取消

				});
			},
		},
		mounted() {
			this.getEmployees();
		}
	}

</script>

<style scoped>

</style>